import { parse } from 'url';

// mock tableListDataSource
const tableListDataSource = [
  {
    callNo: "11000",
    desc: "填空题",
    name: "您有过敏性皮炎史吗？",
    updatedAt: "2019-04-12T14:00:00.000Z",
  },
  {
    callNo: "11001",
    desc: "选择题/填空题",
    name: "上述痒疹出现的部位",
    updatedAt: "2019-04-12T14:00:00.000Z",
  },
  {
    callNo: "11002",
    desc: "选择题",
    name: "近一年上述痒疹有发生过吗？",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "11003",
    desc: "选择题",
    name: "近一年上述痒疹有完全消失过吗？",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "10004",
    desc: "选择题",
    name: "近一年有你夜间有多少次因疹子发痒难以入睡",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "10005",
    desc: "选择题",
    name: "家族有吸烟史吗？",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "10006",
    desc: "选择题",
    name: "您本人近一年内有吸烟史吗？",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "10007",
    desc: "选择题",
    name: "鼻炎症状的发作时间--一年内发生过几次？",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "10008",
    desc: "选择题",
    name: "鼻炎症状的发作时间--一周内发生过几次？",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "10009",
    desc: "选择题/填空题",
    name: "吃过过敏药物吗？",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "10010",
    desc: "选择题",
    name: "有进行脱敏治疗吗？",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "10011",
    desc: "选择题",
    name: "最早的一次发病是在哪一年？",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "10012",
    desc: "选择题",
    name: "最早一次使用脱敏针是哪一年？",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "10013",
    desc: "选择题",
    name: "您的父母有过敏史吗？",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  {
    callNo: "10014",
    desc: "选择题",
    name: "您的兄弟姐妹有过敏史吗？",
    updatedAt: "2019-04-12T16:00:00.000Z",
  },
  
];
// for (let i = 0; i < 46; i += 1) {
//   tableListDataSource.push({
//     key: i,
//     disabled: i % 6 === 0,
//     href: 'https://ant.design',
//     avatar: [
//       'https://gw.alipayobjects.com/zos/rmsportal/eeHMaZBwmTvLdIwMfBpg.png',
//       'https://gw.alipayobjects.com/zos/rmsportal/udxAbMEhpwthVVcjLXik.png',
//     ][i % 2],
//     name: `TradeCode ${i}`,
//     title: `一个任务名称 ${i}`,
//     owner: '曲丽丽',
//     desc: '这是一段描述',
//     callNo: Math.floor(Math.random() * 1000),
//     status: Math.floor(Math.random() * 10) % 4,
//     updatedAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
//     createdAt: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
//     progress: Math.ceil(Math.random() * 100),
//   });
// }

function getTuoMinZhen(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const params = parse(url, true).query;

  let dataSource = tableListDataSource;

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }
      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
      );
    });
    dataSource = filterDataSource;
  }

  if (params.name) {
    dataSource = dataSource.filter(data => data.name.indexOf(params.name) > -1);
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  return res.json(result);
}

export default {
  'GET /api/tuominzhen': getTuoMinZhen,
};
